package com.example.fengsam.aidong.fragment;

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AbsListView;
import android.widget.Toast;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.example.fengsam.aidong.R;
import com.example.fengsam.aidong.activity.MoodActivity;
import com.example.fengsam.aidong.adapter.SocialAdapter;
import com.example.fengsam.aidong.bean.SocialInfo;
import com.example.fengsam.aidong.utils.GlideImageLoader;
import com.example.fengsam.aidong.utils.Screen;
import com.yalantis.phoenix.PullToRefreshView;
import com.youth.banner.Banner;
import com.youth.banner.BannerConfig;
import com.youth.banner.listener.OnBannerListener;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.Unbinder;


public class SocialFragment extends Fragment implements BaseQuickAdapter.RequestLoadMoreListener, OnBannerListener {


    @BindView(R.id.rv_social_fm)
    RecyclerView rvSocialFm;
    @BindView(R.id.pull_to_refresh)
    PullToRefreshView mPullToRefreshView;
    //@BindView(R.id.social_banner)
    Banner banner;
    Unbinder unbinder;

    SocialAdapter mAdapter;
    List<SocialInfo> socialInfos = new ArrayList<SocialInfo>();

    private static final int PAGE_SIZE = 9;
    private int mCurrentCounter = 0;
    private static final int TOTAL_COUNTER = 100;
    private boolean isErr = true;
    private boolean mLoadMoreEndGone = false;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {

        View view = inflater.inflate(R.layout.fragment_social, container, false);
        unbinder = ButterKnife.bind(this, view);
        initView();
        initData();
        initAdapter();
        initListener();
        return view;
    }

    public void initView() {
        //下拉刷新
        mPullToRefreshView.setOnRefreshListener(new PullToRefreshView.OnRefreshListener() {
            @Override
            public void onRefresh() {
                mPullToRefreshView.postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        mPullToRefreshView.setRefreshing(false);
                    }
                }, 2000);
            }
        });
        //rv初始化,避免内容显示不出来
        rvSocialFm.setHasFixedSize(true);
        rvSocialFm.setLayoutManager(new LinearLayoutManager(getActivity()));

    }

    private void addHeadView() {
        View header = LayoutInflater.from(getActivity()).inflate(R.layout.social_banner, null);
        banner=(Banner)header.findViewById(R.id.banner);
         banner.setLayoutParams(new AbsListView.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, Screen.getScreen(false,getActivity())/4));
        //banner
        banner.setBannerTitles(Arrays.asList(getResources().getStringArray(R.array.title)));
        banner.setBannerStyle(BannerConfig.CIRCLE_INDICATOR_TITLE);
        banner.setImages(Arrays.asList(getResources().getStringArray(R.array.url)))
                .setImageLoader(new GlideImageLoader())
                .setOnBannerListener(this)
                .start();
        mAdapter.addHeaderView(header);
    }


    public void initData() {
        for (int i = 0; i < 10; i++) {
            SocialInfo si = new SocialInfo(String.valueOf(i));
            socialInfos.add(si);
        }
    }

    public List<SocialInfo> loadMoreData(int pagesize) {
        List<SocialInfo> list = new ArrayList<>();
        for (int i = 0; i < pagesize; i++) {
            SocialInfo si = new SocialInfo(String.valueOf(i));
            list.add(si);
        }
        return list;
    }

    public void initAdapter() {
        mAdapter = new SocialAdapter(R.layout.social_item, socialInfos);
        mAdapter.setOnLoadMoreListener(this, rvSocialFm);
        mAdapter.openLoadAnimation(BaseQuickAdapter.SLIDEIN_LEFT);
        mAdapter.isFirstOnly(false);
        rvSocialFm.setAdapter(mAdapter);
        addHeadView();
    }

    public void initListener()
    {
        mAdapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                switch (position)
                {
                    case 0:
                        startActivity(new Intent(getActivity(), MoodActivity.class));
                        break;
                    case 1:

                        break;
                }
            }
        });
    }


    @Override
    public void onDestroyView() {
        super.onDestroyView();
        unbinder.unbind();
    }

    @Override
    public void onLoadMoreRequested() {
        if (mAdapter.getData().size() < PAGE_SIZE) {
            mAdapter.loadMoreEnd(false);
        } else {
            if (mCurrentCounter >= TOTAL_COUNTER) {
//                    pullToRefreshAdapter.loadMoreEnd();//default visible
                mAdapter.loadMoreEnd(mLoadMoreEndGone);//true is gone,false is visible
            } else {
                if (isErr) {
                    mAdapter.addData(loadMoreData(PAGE_SIZE));
                    mCurrentCounter = mAdapter.getData().size();
                    mAdapter.loadMoreComplete();
                } else {
                    isErr = true;
                    mAdapter.loadMoreFail();

                }
            }
        }
    }

    @Override
    public void OnBannerClick(int position) {
        Toast.makeText(getActivity(), "你点击了：" + position, Toast.LENGTH_SHORT).show();
    }
}
